home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / usr (gcc 1.37 libs) / mac / console.c < prev    next >
Text File  |  1993-12-13  |  12KB  |  507 lines

  1. /*******************************************************************\
  2. * Console driver based on    
  3. *    the sample application from Inside Macintosh (RoadMap p.15-17)
  4. *
  5. \*******************************************************************/
  6.  
  7. #include <Desk.h>
  8. #include <Scrap.h>
  9. #include <Fonts.h>
  10. #include <Memory.h>
  11. #include <OSEvents.h>
  12. #include <ToolUtils.h>
  13. #include <SegLoad.h>
  14. #include <Windows.h>
  15. #include <Dialogs.h>
  16. #include <Events.h>
  17. #include <Menus.h>
  18. #include <TextEdit.h>
  19.  
  20. #define appleID            128
  21. #define fileID            129
  22. #define editID            130
  23.  
  24. /* Edit menu command indices */
  25. #define undoCommand     1
  26. #define cutCommand        3
  27. #define copyCommand        4
  28. #define pasteCommand    5
  29. #define clearCommand    6
  30. #define selallCommand   7
  31.  
  32. /* Menu indices */
  33. #define appleM            0
  34. #define fileM            1
  35. #define editM            2
  36.  
  37. #define SBarWidth    15
  38.  
  39. int DoCommand(long mResult);
  40.  
  41. #include "crtlocal.h"
  42.  
  43. static WindowRecord        crt_wRecord;
  44. static int                linesInFolder;
  45. static MenuHandle        myMenus[3];
  46. static ControlHandle     vScroll;
  47.  
  48. #define    ours(w)        ((crt_myWindow != NULL) && (w == crt_myWindow))
  49.  
  50. int SetUpWindows(Str255 name)
  51. {
  52.     Rect        viewRect;
  53.     FontInfo    myInfo;
  54.     GrafPort    myPort;
  55.  
  56.     OpenPort(&myPort);
  57.     SetPort(&myPort);
  58.     
  59.     TextFont(monaco);
  60.     TextSize(9);
  61.     GetFontInfo(&myInfo);
  62.     ClosePort(&myPort);
  63.  
  64.     viewRect.left = 0;
  65.     viewRect.top = 32;
  66.     viewRect.right = viewRect.left+82*myInfo.widMax+SBarWidth;
  67.     viewRect.bottom = viewRect.top+25*(myInfo.ascent+myInfo.descent+myInfo.leading)+SBarWidth;
  68.     OffsetRect(&viewRect,
  69.             (qd.screenBits.bounds.right-viewRect.right)/2,
  70.             (qd.screenBits.bounds.bottom-viewRect.bottom)/2);
  71.     crt_myWindow = NewWindow(&crt_wRecord,&viewRect,name,1,0,(WindowPtr) -1L,1,0);
  72.     SetPort(crt_myWindow);
  73.     TextFont(monaco);
  74.     TextSize(9);
  75.     
  76.     viewRect = qd.thePort->portRect;
  77.     viewRect.left = viewRect.right-15;
  78.     viewRect.right += 1;
  79.     viewRect.bottom -= 14;
  80.     viewRect.top -= 1;
  81.     vScroll = NewControl( crt_myWindow, &viewRect, "\p", 1, 0, 0, 0, scrollBarProc, 0L);
  82.     
  83.     viewRect = qd.thePort->portRect;
  84.     viewRect.right -= SBarWidth;
  85.     viewRect.bottom -= SBarWidth;
  86.     InsetRect(&viewRect, 4, 4);
  87.  
  88.     crt_TEH = TEStylNew(&viewRect, &viewRect);
  89.     (**crt_TEH).viewRect = qd.thePort->portRect;
  90.     (**crt_TEH).viewRect.right -= SBarWidth;
  91.     (**crt_TEH).viewRect.bottom -= SBarWidth;
  92.     InsetRect(&(**crt_TEH).viewRect, 4, 4);
  93.  
  94.     linesInFolder = ((**crt_TEH).viewRect.bottom-(**crt_TEH).viewRect.top)/(**crt_TEH).lineHeight;
  95.     (**crt_TEH).viewRect.bottom = (**crt_TEH).viewRect.top + (**crt_TEH).lineHeight*linesInFolder;
  96.     (**crt_TEH).destRect.right = (**crt_TEH).viewRect.right;
  97.     TECalText(crt_TEH);
  98. }
  99.  
  100. int AdjustText (void)
  101.  
  102. {
  103.     int        oldScroll, newScroll, delta;
  104.     
  105.     oldScroll = (**crt_TEH).viewRect.top - (**crt_TEH).destRect.top;
  106.     newScroll = GetCtlValue(vScroll) * (**crt_TEH).lineHeight;
  107.     delta = oldScroll - newScroll;
  108.     if (delta != 0)
  109.       TEScroll(0, delta, crt_TEH);
  110. }
  111.  
  112. #ifdef SCROLLPROC
  113.  
  114. pascal void ScrollProc (ControlHandle theControl, int theCode)
  115. {
  116.     int    pageSize;
  117.     int    scrollAmt;
  118.     int oldCtl;
  119.     
  120.     if (theCode == 0)
  121.         return ;
  122.     
  123.     pageSize = ((**crt_TEH).viewRect.bottom-(**crt_TEH).viewRect.top) / 
  124.             (**crt_TEH).lineHeight - 1;
  125.             
  126.     switch (theCode) {
  127.         case inUpButton: 
  128.             scrollAmt = -1;
  129.             break;
  130.         case inDownButton: 
  131.             scrollAmt = 1;
  132.             break;
  133.         case inPageUp: 
  134.             scrollAmt = -pageSize;
  135.             break;
  136.         case inPageDown: 
  137.             scrollAmt = pageSize;
  138.             break;
  139.     }
  140.  
  141.     oldCtl = GetCtlValue(theControl);
  142.     SetCtlValue(theControl, oldCtl+scrollAmt);
  143.  
  144.     AdjustText();
  145.  
  146. }
  147.  
  148. #endif
  149.  
  150. int DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  151.  
  152. {
  153.     switch (windowPart) {
  154.         case inGoAway:
  155.             if (ours(whichWindow))
  156.                 if (TrackGoAway(crt_myWindow, myEvent->where))
  157.                         {
  158.                         HideWindow(crt_myWindow);
  159.                         TESetSelect(0, (**crt_TEH).teLength, crt_TEH);
  160.                         TEDelete(crt_TEH);
  161.                             {
  162.                                 register int    n;
  163.                                 
  164.                                 n = (**crt_TEH).nLines-linesInFolder;
  165.                             
  166.                                 if ((**crt_TEH).teLength > 0 && (*((**crt_TEH).hText))[(**crt_TEH).teLength-1]=='\r')
  167.                                     n++;
  168.                             
  169.                                 SetCtlMax(vScroll, n > 0 ? n : 0);
  170.                             }
  171.                         }
  172.             break;
  173.  
  174.         case inMenuBar:
  175.             return(DoCommand(MenuSelect(myEvent->where)));
  176.  
  177.         case inSysWindow:
  178.             SystemClick(myEvent, whichWindow);
  179.             break;
  180.  
  181.         case inDrag:
  182.             if (ours(whichWindow))
  183.                 {
  184.                 Rect dragRect = { 0, 0, 1024, 1024 };
  185.                 DragWindow(whichWindow, myEvent->where, &dragRect);
  186.                 }
  187.             break;
  188.  
  189.         case inGrow:
  190.             if (ours(whichWindow))
  191.                 {
  192.                     Point p = myEvent->where;
  193.                     GrafPtr    savePort;
  194.                     long    theResult;
  195.                     Rect    oldHorizBar;
  196.                     Rect     r;
  197.                     
  198.                     GetPort(&savePort);
  199.                     SetPort(whichWindow);
  200.                     
  201.                     oldHorizBar = whichWindow->portRect;
  202.                     oldHorizBar.top = oldHorizBar.bottom - (SBarWidth+1);
  203.                 
  204.                     SetRect(&r, 80, 80, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom);
  205.                     theResult = GrowWindow(whichWindow, p, &r);
  206.                     if (theResult == 0)
  207.                       return;
  208.                     SizeWindow( whichWindow, LoWord(theResult), HiWord(theResult), false);
  209.                 
  210.                     InvalRect(&whichWindow->portRect);
  211.                     
  212.                     (**crt_TEH).viewRect = whichWindow->portRect;
  213.                     (**crt_TEH).viewRect.right -= SBarWidth;
  214.                     (**crt_TEH).viewRect.bottom -= SBarWidth;
  215.                     InsetRect(&(**crt_TEH).viewRect, 4, 4);
  216.                 
  217.                     linesInFolder = ((**crt_TEH).viewRect.bottom-(**crt_TEH).viewRect.top)/(**crt_TEH).lineHeight;
  218.                     (**crt_TEH).viewRect.bottom = (**crt_TEH).viewRect.top + (**crt_TEH).lineHeight*linesInFolder;
  219.                     (**crt_TEH).destRect.right = (**crt_TEH).viewRect.right;
  220.                     TECalText(crt_TEH);
  221.  
  222.                     EraseRect(&oldHorizBar);
  223.                     
  224.                     MoveControl(vScroll, whichWindow->portRect.right - SBarWidth, whichWindow->portRect.top-1);
  225.                     SizeControl(vScroll, SBarWidth+1, whichWindow->portRect.bottom - whichWindow->portRect.top-(SBarWidth-2));
  226.                     r = (**vScroll).contrlRect;
  227.                     ValidRect(&r);
  228.                 
  229.                 
  230.                         {
  231.                             register int    n;
  232.                             
  233.                             n = (**crt_TEH).nLines-linesInFolder;
  234.                         
  235.                             if ((**crt_TEH).teLength > 0 && (*((**crt_TEH).hText))[(**crt_TEH).teLength-1]=='\r')
  236.                                 n++;
  237.                         
  238.                             SetCtlMax(vScroll, n > 0 ? n : 0);
  239.                         }
  240.  
  241.                     AdjustText();
  242.                     
  243.                     SetPort(savePort);
  244.                 }
  245.             break;
  246.  
  247.         case inContent:
  248.             if (whichWindow != FrontWindow())
  249.                 SelectWindow(whichWindow);
  250.             else if (ours(whichWindow))
  251.                 {
  252.                 int                cntlCode;
  253.                 ControlHandle     theControl;
  254.                 int                pageSize;
  255.                 GrafPtr            savePort;
  256.                 
  257.                 GetPort(&savePort);
  258.                 SetPort(whichWindow);
  259.                 
  260.                 GlobalToLocal(&myEvent->where);
  261.                 if ((cntlCode = FindControl(myEvent->where, whichWindow, &theControl)) == 0) {
  262.                 if (PtInRect(myEvent->where, &(**crt_TEH).viewRect))
  263.                     TEClick(myEvent->where, (myEvent->modifiers & shiftKey)!=0, crt_TEH);
  264.                 } else if (cntlCode == inThumb) {
  265.                 TrackControl(theControl, myEvent->where, 0L);
  266.                 AdjustText();
  267.                 } else
  268.                 TrackControl(theControl, myEvent->where, 
  269. #ifdef SCROLLPROC                
  270. &ScrollProc);
  271. #else
  272. 0);
  273. #endif
  274.                 SetPort(savePort);
  275.                 }
  276.             break;
  277.     }
  278. }
  279.  
  280.  
  281. int ShowSelect (void)
  282.  
  283. {
  284.     register    int        topLine, bottomLine, theLine;
  285.     
  286.     register int    n;
  287.     
  288.     n = (**crt_TEH).nLines-linesInFolder;
  289.  
  290.     if ((**crt_TEH).teLength > 0 && (*((**crt_TEH).hText))[(**crt_TEH).teLength-1]=='\r')
  291.         n++;
  292.  
  293.     SetCtlMax(vScroll, n > 0 ? n : 0);
  294.     AdjustText();
  295.     
  296.     topLine = GetCtlValue(vScroll);
  297.     bottomLine = topLine + linesInFolder;
  298.     
  299.     if ((**crt_TEH).selStart < (**crt_TEH).lineStarts[topLine] ||
  300.             (**crt_TEH).selStart >= (**crt_TEH).lineStarts[bottomLine]) {
  301.         for (theLine = 0; (**crt_TEH).selStart >= (**crt_TEH).lineStarts[theLine]; theLine++)
  302.             ;
  303.         SetCtlValue(vScroll, theLine - linesInFolder / 2);
  304.         AdjustText();
  305.     }
  306. }
  307.  
  308. int SetUpMenus(void)
  309. {
  310.     int        i;
  311.     
  312.     myMenus[appleM] = NewMenu(appleID, "\p\024");
  313.     AppendMenu(myMenus[appleM], "\pAbout ...");
  314.     AddResMenu(myMenus[appleM], 'DRVR');
  315.     myMenus[fileM] = NewMenu(fileID, "\pFile");
  316.     AppendMenu(myMenus[fileM], "\pQuit/Q");
  317.     myMenus[editM] = NewMenu(editID, "\pEdit");
  318.     AppendMenu(myMenus[editM], "\pUndo/Z;-;Cut/X;Copy/C;Paste/V;Clear;Select All/A");
  319.     DisableItem(myMenus[editM], undoCommand);
  320.     for ((i=appleM); (i<=editM); i++)
  321.         InsertMenu(myMenus[i], 0) ;
  322.     DrawMenuBar();
  323. }
  324.  
  325. int DoCommand(long mResult)
  326.     {
  327.     int        theItem;
  328.     Str255    name;
  329.     
  330.     theItem = LoWord(mResult);
  331.     switch (HiWord(mResult)) 
  332.         {
  333.         case appleID:
  334.             if (myMenus[appleM])
  335.                 {
  336.                 GetItem(myMenus[appleM], theItem, name);
  337.                 OpenDeskAcc(name);
  338.                 SetPort(crt_myWindow);
  339.                 break;
  340.                 }
  341.         case fileID:
  342.             ExitToShell();
  343.             break;
  344.  
  345.         case editID: 
  346.             if (SystemEdit(theItem-1) == 0) {
  347.                 switch (theItem) {
  348.                     case cutCommand:
  349.                         ZeroScrap();
  350.                         TECut(crt_TEH);
  351.                         break;
  352.     
  353.                     case copyCommand:
  354.                         ZeroScrap();
  355.                         TECopy(crt_TEH);
  356.                         break;
  357.         
  358.                     case pasteCommand:
  359.                         TEPaste(crt_TEH);
  360.                         break;
  361.         
  362.                     case clearCommand:
  363.                         TEDelete(crt_TEH);
  364.                         break;
  365.                         
  366.                     case selallCommand:
  367.                         TESetSelect(0, 32767, crt_TEH);
  368.                         break;
  369.                 }
  370.                 ShowSelect();
  371.             }
  372.             break;
  373.     }
  374.     HiliteMenu(0);
  375.     return(1);
  376. }
  377.  
  378. int MaintainCursor(void)
  379. {
  380.     Point        pt;
  381.     WindowPeek    wPtr;
  382.     GrafPtr        savePort;
  383.     
  384.     if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
  385.         GetPort(&savePort);
  386.         SetPort((GrafPtr)wPtr);
  387.         GetMouse(&pt);
  388.         if (PtInRect(pt, &(**crt_TEH).viewRect ) )
  389.             SetCursor( *GetCursor(1));
  390.         else SetCursor(&qd.arrow);
  391.         SetPort(savePort);
  392.     }
  393. }
  394.  
  395. void test_inited(Str255 name)
  396.     {
  397.     if (!qd.thePort)
  398.         {
  399.         InitGraf(&qd.thePort);
  400.         InitFonts();
  401.         InitWindows();
  402.         InitMenus();
  403.         TEInit();
  404.         InitCursor();
  405.         SetUpMenus();
  406.         }
  407.     if (!crt_myWindow)
  408.         {
  409.         SetUpWindows(name);
  410.         }
  411.     }
  412.  
  413. int cwrite(int fd, const void *buf, int size)
  414.     {
  415.     int tot;
  416.     Ptr newtext;
  417.     test_inited(fd==2?"\pStdErr":"\pStdOut");
  418.     newtext = NewPtr(size);
  419.     if (!newtext) newtext = (Ptr)buf;
  420.     else for (tot = 0; tot < size; tot++)
  421.         {
  422.         newtext[tot] = ((char *)buf)[tot];
  423.         if (newtext[tot]=='\n') newtext[tot]='\r';
  424.         }
  425.     TESetSelect(32767,32767,crt_TEH);
  426.     TEInsert(newtext, size, crt_TEH);
  427.     if (newtext != (Ptr) buf) DisposPtr(newtext);
  428.     return size;
  429.     }
  430.  
  431. int cgetc(void) 
  432.     {
  433.     EventRecord        myEvent;
  434.     WindowPtr        whichWindow;
  435.     short            windowPart;
  436.     Rect            r;
  437.     test_inited("\pStdIn");
  438.     for (;;)
  439.         {
  440.         MaintainCursor();
  441.         SystemTask();
  442.         TEIdle(crt_TEH);
  443.         if (GetNextEvent(everyEvent, &myEvent)) 
  444.             {
  445.             switch (myEvent.what) 
  446.                 {
  447.                 case mouseDown:
  448.                     windowPart = FindWindow(myEvent.where, &whichWindow);
  449.                     DoMouseDown(windowPart, whichWindow, &myEvent);
  450.                     break;
  451.                 case keyDown:
  452.                 case autoKey: 
  453.                     {
  454.                     register unsigned char theChar;
  455.                     theChar = myEvent.message & charCodeMask;
  456.                     if ((myEvent.modifiers & cmdKey) != 0) 
  457.                         DoCommand(MenuKey(theChar));
  458.                     else if (theChar == '\r')
  459.                         return '\n';
  460.                     else if (theChar < ' ') 
  461.                         {
  462.                         TEKey(theChar, crt_TEH);
  463.                         ShowSelect();
  464.                         }
  465.                     else return(theChar);
  466.                     break;
  467.                     }    
  468.                 case activateEvt:
  469.                     if (ours((WindowPtr)myEvent.message)) 
  470.                         {
  471.                         if (myEvent.modifiers & activeFlag) 
  472.                             {
  473.                             TEActivate(crt_TEH);
  474.                             ShowControl(vScroll);
  475.                             }
  476.                         else 
  477.                             {
  478.                             TEDeactivate(crt_TEH);
  479.                             HideControl(vScroll);
  480.                             }
  481.                         }
  482.                     break;
  483.         
  484.                 case updateEvt: 
  485.                     if (ours((WindowPtr) myEvent.message))
  486.                             {
  487.                                 GrafPtr    savePort;
  488.                                 
  489.                                 GetPort(&savePort);
  490.                                 SetPort(crt_myWindow);
  491.                             
  492.                                 BeginUpdate(crt_myWindow);
  493.                                 EraseRect(&crt_myWindow->portRect);
  494.                                 DrawControls(crt_myWindow);
  495.                                 DrawGrowIcon(crt_myWindow);
  496.                                 TEUpdate(&crt_myWindow->portRect, crt_TEH);
  497.                                 EndUpdate(crt_myWindow);
  498.                             
  499.                                 SetPort(savePort);
  500.                             }
  501.                             
  502.                     break;
  503.                 } /* end of case myEvent.what */
  504.             } /* if */
  505.         }
  506.     }
  507.